package com.springcloud.market.api.service;


import com.springcloud.market.api.dto.UserDTO;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.time.Instant;

@Slf4j
@Service
public class UserRegisterMessageSender {
    private final StreamBridge streamBridge;
    public UserRegisterMessageSender(StreamBridge streamBridge) {
        this.streamBridge = streamBridge;
    }
    public void sendUserRegisterMessage(String userId, String username, String phoneNumber) {
        // 构造消息内容
        UserDTO userDTO = new UserDTO(userId, username, phoneNumber, Instant.now().toString());
        // 使用StreamBridge发送消息
        boolean sent = streamBridge.send("userRegister-out-0", MessageBuilder.withPayload(userDTO).build());
        if (sent) {
            log.info("用户注册成功消息已发送，用户ID: {}", userId);
        } else {
            log.error("用户注册成功消息发送失败，用户ID: {}", userId);
        }
    }
}

